package com.yubest.shiro.exception;

import com.yubest.shiro.bean.Response;
import com.yubest.shiro.consts.Consts;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 *
 */
@RestControllerAdvice
@Slf4j
public class RestExceptionHandler {

    @ExceptionHandler(UnknownAccountException.class)
    public <T> Response<T> exceptionHandler(UnknownAccountException e) {
        log.error("UnknownAccountException异常", e);
        return Response.error(Consts.LOGIN_ERROR, "账号不存在");
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public <T> Response<T> exceptionHandler(IncorrectCredentialsException e) {
        log.error("IncorrectCredentialsException异常", e);
        return Response.error(Consts.LOGIN_ERROR, "密码错误");
    }

    @ExceptionHandler(UnauthorizedException.class)
    public <T> Response<T> exceptionHandler(UnauthorizedException e) {
        log.error("UnauthorizedException异常", e);
        return Response.error(Consts.NOT_PERMIT_CODE, "无操作权限");
    }

    @ExceptionHandler(Exception.class)
    public <T> Response<T> exceptionHandler(Exception e) {
        log.error("Exception异常", e);
        String msg = e.getMessage();
        return Response.error(msg != null ? msg : "未知错误");
    }

}
